package com.bonc.modules.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bonc.common.param.BaseParam;
import com.bonc.common.utils.Result;
import com.bonc.modules.item.entity.ItemChartEntity;
import com.bonc.modules.item.entity.ItemDateTypeEntity;
import com.bonc.modules.item.entity.ItemInfoEntity;
import com.bonc.modules.item.param.ItemChartParam;
import com.bonc.modules.item.service.ItemChartService;
import com.bonc.modules.item.service.ItemDateTypeService;
import com.bonc.modules.item.service.ItemInfoService;
import com.bonc.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jinlong
 * @date 2021/7/26
 */
@RestController
@RequestMapping("/item")
public class ItemController extends AbstractController {

    @Autowired
    private ItemInfoService itemInfoService;

    @Autowired
    private ItemDateTypeService itemDateTypeService;

    @Autowired
    private ItemChartService itemChartService;


    @GetMapping("/list")
    @RequiresPermissions("item:list")
    public Result list() {
        LambdaQueryWrapper<ItemInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(ItemInfoEntity::getParItemCode, ItemInfoEntity::getOrderNum);
        List<ItemInfoEntity> itemInfoEntityList = itemInfoService.list(queryWrapper);
        for (ItemInfoEntity itemInfoEntity : itemInfoEntityList) {
            ItemInfoEntity parentMenuEntity = itemInfoService.getById(itemInfoEntity.getParItemCode());
            if (parentMenuEntity != null) {
                itemInfoEntity.setParItemName(parentMenuEntity.getItemName());
            }
        }

        return Result.ok().put("itemList", itemInfoEntityList);
    }


    @GetMapping("/dateType")
    public Result dateType(String itemCode) {

        List<ItemDateTypeEntity> itemDateTypeEntityList = itemDateTypeService.getDateType(itemCode);
        return Result.ok().put("dateTypeList", itemDateTypeEntityList);
    }

    @GetMapping("/chartList")
    public Result chartList(@Validated(BaseParam.list.class) ItemChartParam itemChartParam) {

        List<ItemChartEntity> chartList = itemChartService.getChartList(itemChartParam);
        return Result.ok().put("list", chartList);
    }


}
